package com.mobile.tools;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;


public class POIExcel<T> {
	public void exportExcel(String title,OutputStream os,List<T> list,Class clazz)
		throws IOException{
		try {
			BeanInfo bi=Introspector.getBeanInfo(clazz,Object.class);
			PropertyDescriptor[] pds=bi.getPropertyDescriptors();
			//创建新的Excel工作簿
			SimpleDateFormat datetemp = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
			HSSFWorkbook workBook=new HSSFWorkbook();
			//在Excel工作薄中建一工作表,
			HSSFSheet sheet=workBook.createSheet("第一页 ");
			/*
			 *创建datecell的类型 
			 */
			HSSFCellStyle dateCellStyle = workBook.createCellStyle();
			dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("yyyy-MM-DD HH:mm:ss"));
			
			HSSFRow row = sheet.createRow((short) 0);
		    HSSFCell cell = row.createCell((short) 0);
		    
		    HSSFCellStyle titleStyle = workBook.createCellStyle();
		    HSSFFont font = workBook.createFont();
		    font.setFontHeightInPoints((short)28);
		    font.setFontName("Courier New");
		    font.setItalic(true);
		    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		    titleStyle.setFont(font);
		    titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		    
		    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		    cell.setCellValue(title);
		    cell.setCellStyle(titleStyle);
		  	    
		    sheet.addMergedRegion(new Region(0,(short)0,3,(short)(pds.length-1)));
			
			row=sheet.createRow((short)4);
			
			List<String> fields=new ArrayList<String>();
			/*
			 * 设置栏位的style
			 */
			HSSFCellStyle columnStyle = workBook.createCellStyle();
	        HSSFFont boldFont = workBook.createFont();
	        boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	        columnStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	        columnStyle.setFont(boldFont);
			/*
			 * 读取类中的字段 
			 */
			for(int j=0;j<pds.length;j++){
				cell=row.createCell((short)j);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				//中文编码
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(pds[j].getName());
				cell.setCellStyle(columnStyle);
				fields.add(pds[j].getName());
			}
			/*
			 * 读取list中的数据
			 */
			short i=5;
			for(T bean:list){
				row=sheet.createRow(i);
				short j=0;
				for(String field:fields){
					PropertyDescriptor pd=new PropertyDescriptor(field,clazz);
					cell=row.createCell(j);
					cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					cell.setEncoding(HSSFCell.ENCODING_UTF_16);
					
					Object value=pd.getReadMethod().invoke(bean);
					if(value!=null){
						cell.setCellValue(value.toString());
					}else{
						cell.setCellValue("");
					}
					j++;
				}
				i++;
			}
			workBook.write(os);
			os.flush();
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e){
			e.printStackTrace();
		} catch (IllegalAccessException e){
			e.printStackTrace();
		}
	}
}
